package myPackage;
import net.Group.PrivatePeerGroup;
import net.Group.MembershipService.SecureCredential;
import net.jxta.credential.Credential;
import net.jxta.document.MimeMediaType;
import net.jxta.endpoint.Message;
import net.jxta.endpoint.MessageElement;
import net.jxta.logging.Logging;
import net.jxta.membership.MembershipService;
import net.jxta.peer.PeerID;
import net.jxta.peergroup.PeerGroup;
import net.jxta.pipe.PipeMsgEvent;
import net.jxta.pipe.PipeMsgListener;
import net.jxta.platform.NetworkManager;
import net.jxta.util.JxtaBiDiPipe;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.jxta.endpoint.StringMessageElement;
import net.jxta.protocol.PipeAdvertisement;

public class SecurePipeClient implements Runnable, PipeMsgListener {
	
    private final PeerGroup peergroup;
    private PrivatePeerGroup ppg;
    private JxtaBiDiPipe pipe = null;
    private Message msg;
    private final String SECURE_PIPE_ROLE_REQUEST = "request_role";
    private final String SECURE_PIPE_PERM_REQUEST = "request_perm";
    
    private SecurePipeClient(PrivatePeerGroup pg) {
        this.ppg = pg;
        peergroup = pg.getPeerGroup();
    }
    
    private void SendRequestMessage(String Type, PeerID peerid)
    {
    	//Send request msg
    	//peer name||peer id
    	Message msg =new Message();
    	
    	if (Type == SECURE_PIPE_ROLE_REQUEST){
    		MessageElement me1 = new StringMessageElement(SecurePipeServer.MESSAGE_TYPE,SecurePipeServer.MESSAGE_ELEMENT_AUTHORIZATION_ROLE,null);
    		MessageElement me2 = new StringMessageElement(SecurePipeServer.MESSAGE_PEER_ID,peerid.toString(),null);
    		msg.addMessageElement(SecurePipeServer.MESSAGE_NAMESPACE_NAME,me1);
    		msg.addMessageElement(SecurePipeServer.MESSAGE_NAMESPACE_NAME,me2);
    		try{
    		pipe.sendMessage(msg);
    		}catch(Exception e){}
    	}
    	
    	
    }
    
    public void pipeMsgEvent(PipeMsgEvent event) 
    {
    	Message msg = event.getMessage();
    	
    	if(null==msg)
    	{
    		System.out.println("Null message received!");
    	}
    	
    	MessageElement me1 = msg.getMessageElement(SecurePipeServer.MESSAGE_NAMESPACE_NAME,SecurePipeServer.RESPONSE_ELEMENT_NAME );
    	
    	if((me1!= null)&&(me1.toString()!=null))
    	{
    		String type = msg.getMessageElement(SecurePipeServer.MESSAGE_NAMESPACE_NAME,SecurePipeServer.MESSAGE_TYPE).toString();
    		if(type == SecurePipeServer.MESSAGE_ELEMENT_AUTHORIZATION_ROLE)
    		{
    			System.out.println("Role granting message received, start to process..");
    			String peer_id = msg.getMessageElement(SecurePipeServer.MESSAGE_NAMESPACE_NAME,SecurePipeServer.MESSAGE_PEER_ID).toString();
    			String role_id = msg.getMessageElement(SecurePipeServer.MESSAGE_NAMESPACE_NAME,SecurePipeServer.MESSAGE_ROLE_ID).toString();
    			String role_cert = msg.getMessageElement(SecurePipeServer.MESSAGE_NAMESPACE_NAME,SecurePipeServer.MESSAGE_ROLE_CERT).toString();
    			
    			if (verifiedRoleCert(peer_id,role_id,role_cert))
    			{
    				SecureCredential scred = (SecureCredential)ppg.getFinalCredential();
    				scred.setRoleID(role_id);
    				scred.setRoleCert(role_cert.getBytes());
    			}
    			
    		}
    		//TODO: handle the Permission Message in here	
    	
    	}
    	else
    		System.out.println("Message Error:missing element or null element");
    	
    }
    
    private boolean verifiedRoleCert(String peerid,String roleid, String role_cert)
    {
    	return true;
    }
    
    public void run()
    {
    	// get the adv though search from pipe id
    	PipeAdvertisement connect_pipe = SecurePipeServer.getPipeAdvertisement();
    	try{
    		
    		pipe = new JxtaBiDiPipe(peergroup, connect_pipe, 0, this, true);
    		SecureCredential scred = (SecureCredential)ppg.getFinalCredential();
    		pipe.setCredentialDoc(scred.getDocument(new MimeMediaType("text/plain")));
    		
    		
    	}catch(Exception e)
    	{
    		
    	}
    	
    	
    }
    // way to use the thread
    // Thread thread = new Thread(new SecurePipeClient(privatepeergroup));
    // thread.start();
}
